unit uDisplay.local;

interface

uses ComCTRLS, ExtCtrls,

     uFuncs.local, uGame.local, uDefs.local;

procedure SetupRaceOverview(var nListView:tListView;TotalDrivers:integer);
procedure DisplayRaceOverview(var Game:tGame; var nListView:tListView;HighLight:integer;ShowFuelLoad:boolean);
procedure SetupDriverPodium(var nListView:tListView);
procedure DisplayDriverPodium(var Game:tGame;var nListView:tListView;HighLight:string);
procedure SetupTeamPodium(var nListView:tListView);
procedure DisplayTeamPodium(var Game:tGame;var nListView:tListView;HighLight:string);
procedure DisplayTyreTOZ(var nGame:TGame;var nImage:TImage);

implementation

uses SysUtils, Graphics;

procedure addColumn(var nListView:tListView;nColumnName:string;nRow,nColumnID:integer);
begin
  with nListView do
  begin
    Column[nColumnID].Caption := nColumnName;
    Items[nRow].SubItems.Add('');
  end;
end;

procedure SetupRaceOverview(var nListView:tListView;TotalDrivers:integer);
var D : integer;
begin
  with nListView do
  begin
    Items.Clear;
    for D := 0 to TotalDrivers-1 do
    begin
      Items.Add;
      AddColumn(nListView, '#', D, 0);
      AddColumn(nListView, 'Gain', D, 1);
      AddColumn(nListView, 'Driver', D, 2);
      AddColumn(nListView, 'LapTime', D, 3);
      AddColumn(nListView, 'Next', D, 4);
      AddColumn(nListView, 'LapDiff', D, 5);
      AddColumn(nListView, 'Leader', D, 6);
      AddColumn(nListView, 'LeadDiff', D, 7);
      AddColumn(nListView, 'BestLap', D, 8);
      AddColumn(nListView, 'TotalTime', D, 9);
      AddColumn(nListView, 'Status', D, 10);
      AddColumn(nListView, 'Tyres', D, 11);
      AddColumn(nListView, 'Pits', D, 12);
      AddColumn(nListView, 'Laps', D, 13);
      AddColumn(nListView, 'Fuel', D, 14);
      AddColumn(nListView, 'KPH' , D, 15);
    end;
  end;
end;

procedure DisplayRaceOverview(var Game:tGame;var nListView:tListView;HighLight:integer;ShowFuelLoad:boolean);
var
  D         : integer;
  nDriverID : integer;
  SecondsToTime : TSecondsToTime;
begin
  SecondsToTime := TSecondsToTime.Create();

  with Game do
  with nListView do
  begin
    for D := 0 to Game.DriverList.Count-1 do
    begin
      nDriverID := Game.Race.Grid[ D ];
      with Items[D] do
      begin
        Caption := IntToStr(D+1);
        with SubItems do
        begin
          if (Game.Race.Lap > 0) then
            Strings[0] := IntToStr( Driver(nDriverID).PreviousTrackPosition - Driver(nDriverID).TrackPosition )
          else
            Strings[0] := '-';

          if (nDriverID = HighLight) then
          begin
            Strings[1] := '**'+Driver(nDriverID).Name;
//            Strings[1] := '**'+Driver(nDriverID).Name+' '+IntToStr(Driver(nDriverID).DriverSmooth)+'/'+FormatFloat('0.00', Driver(nDriverID).TyreHealth);
            nListView.GridLines := TRUE;//.ItemIndex := D;
            nListView.ItemIndex := D;
          end
          else
            Strings[1] := Driver(nDriverID).Name;
//            Strings[1] := Driver(nDriverID).Name+' '+IntToStr(Driver(nDriverID).DriverSmooth)+'/'+FormatFloat('0.00', Driver(nDriverID).TyreHealth);

          if (not Driver(nDriverID).IsBot) then
            Strings[1] := Strings[1] + '* '+IntToStr(Driver(nDriverID).DriverSmooth)+'/'+FormatFloat('0.00', Driver(nDriverID).TyreHealth);

          Strings[2] := SecondsToTime.Convert( Driver(nDriverID).LapTime );

          Strings[3] := SecondsToTime.Convert( Driver(nDriverID).TimeToNextDriver );

          if (Driver(nDriverID).LapTimeDiff > 0) then
            Strings[4] := '+'+SecondsToTime.Convert( Driver(nDriverID).LapTimeDiff )
          else
            Strings[4] := SecondsToTime.Convert( Driver(nDriverID).LapTimeDiff );

          Items[D].SubItems.Strings[5] := SecondsToTime.Convert( Driver(nDriverID).TimeToLeader );

          if (Driver(nDriverID).LeaderLapTimeDiff > 0) then
            Items[D].SubItems.Strings[6] := '+'+SecondsToTime.Convert( Driver(nDriverID).LeaderLapTimeDiff )
          else
            Items[D].SubItems.Strings[6] := SecondsToTime.Convert( Driver(nDriverID).LeaderLapTimeDiff );

          Strings[7] := SecondsToTime.Convert( Driver(nDriverID).FastestLap.Time );
          Strings[8] := SecondsToTime.Convert( Driver(nDriverID).TotalTime );

          with Driver(nDriverID) do
          begin
            if (STATUS_RACING     in Status) then Strings[9] := 'RACING';
            if (STATUS_PITTING    in Status) then Strings[9] := 'PITTING';
            if (STATUS_PIT        in Status) then Strings[9] := 'PIT';
            if (STATUS_OUTLAP     in Status) then Strings[9] := 'OUTLAP';

            if (STATUS_DIRTYAIR   in Status) then Strings[9] := Strings[9]+'-';
            if (STATUS_PIGEON     in Status) then Strings[9] := Strings[9]+'+';
            if (STATUS_SLIPSTREAM in Status) then Strings[9] := Strings[9]+'++';
            if (STATUS_BOOST      in Status) then Strings[9] := Strings[9]+'+++';

            if (STATUS_SPIN       in Status) then Strings[9] := Strings[9]+' [SPIN]';
            if (STATUS_PUNCTURE   in Status) then Strings[9] := Strings[9]+' [PUNCTURE]';
            if (STATUS_OFFROAD    in Status) then Strings[9] := Strings[9]+' [OFFROAD]';
            if (STATUS_PENALTY    in Status) then Strings[9] := Strings[9]+' [PENALTY]';
            if (STATUS_CRASH      in Status) then Strings[9] := Strings[9]+' [ACCIDENT]';
            if (STATUS_LOCKUP     in Status) then Strings[9] := Strings[9]+' [LOCKUP]';
            if (STATUS_PITERROR   in Status) then Strings[9] := Strings[9]+' [P--]';
            if (STATUS_GRID       in Status) then Strings[9] := ' GRID';
          end;

          Strings[10] := Tyre(Driver(nDriverID).Tyres).Name+' ['+IntToStr(Driver(nDriverID).Tyres)+']';

          Strings[11] := IntToStr( Driver(nDriverID).TotalStops );

          if (ShowFuelLoad)or(nDriverID = HighLight)or(Driver(nDriverID).TeamID = Driver(HighLight).TeamID) then
          begin
            Strings[12] := FormatFloat('00',Driver(nDriverID).FuelLaps);
            Strings[13] := FormatFloat('00.000', Driver(nDriverID).Fuel);
          end
          else
          begin
            Strings[12] := '-';
            Strings[13] := '-';
          end;
          Strings[14] := FormatFloat('00.000', Driver(nDriverID).Velocity );
        end;
      end;
    end;
  end;
  FreeAndNil( SecondsToTime );
end;

function FastestLap(var Game:TGame):string;
var Sec2Time:TSecondsToTime;
begin
  Sec2Time := TSecondsToTime.Create();

  with Game do
    result := Driver( Game.Race.FastestDriver.ID ).Name+' [Time] '+
              Sec2Time.Convert( Driver(Game.Race.FastestDriver.ID).LapTime )+' [Lap] '+
              IntToStr( Game.Race.FastestDriver.Lap );

  FreeAndNil( Sec2Time );
end;

procedure SetupDriverPodium(var nListView:tListView);
var D : integer;
begin
  with Game do
  with nListView do
  begin
    Items.Clear;
//    AddColumn(nListView, '#', D, 0);
//    AddColumn(nListView, 'Driver', D, 1);
//    AddColumn(nListView, 'Points', D, 2);

    for D := 0 to High(Season.DriverStandings ) do
    begin
      Items.Add;
      Items[D].SubItems.Add('');
      Items[D].SubItems.Add('');
      Items[D].SubItems.Add('');
    end;
  end;
end;

procedure DisplayDriverPodium(var Game:tGame;var nListView:tListView;HighLight:string);
var D, nID : integer;
begin
  with Game do
  with nListView do
  begin
    for D := 0 to High( Season.DriverStandings ) do
    begin
      nID := Season.DriverStandings[ D ].Index;
      with Items[ D ] do
      begin
        Caption := IntToStr(D+1);
        with SubItems do
        begin
          if (Driver( nID ).Name = HighLight) then
            Strings[0] := '**'+Driver( nID ).Name
          else
            Strings[0] := Driver( nID ).Name;
          Strings[1] := IntToStr( Driver( nID ).Points );
        end;
      end;
    end;
  end;
end;

procedure SetupTeamPodium(var nListView:tListView);
var D : integer;
begin
  with Game do
  with nListView do
  begin
    Items.Clear;
//    AddColumn(nListView, '#', D, 0);
//    AddColumn(nListView, 'Driver', D, 1);
//    AddColumn(nListView, 'Points', D, 2);

    for D := 0 to High(Season.TeamStandings ) do
    begin
      Items.Add;
      Items[D].SubItems.Add('');
      Items[D].SubItems.Add('');
      Items[D].SubItems.Add('');
    end;
  end;
end;

procedure DisplayTeamPodium(var Game:tGame;var nListView:tListView;HighLight:string);
var T, nID : integer;
begin
  with Game do
  with nListView do
  begin
    for T := 0 to High( Season.TeamStandings ) do
    begin
      nID := Season.TeamStandings[ T ].Index;
      with Items[ T ] do
      begin
        Caption := IntToStr(T+1);
        with SubItems do
        begin
//          if (Driver( nID ).Name = HighLight) then
//            Strings[0] := '**'+Driver( nID ).Name
//          else
          Strings[0] := Team( nID ).Name;
          Strings[1] := IntToStr( Team( nID ).Points );
        end;
      end;
    end;
  end;
end;

procedure DisplayTyreTOZ(var nGame:TGame;var nImage:TImage);
var T, W, TyreOpZone, TestGrip, NewColor, Radius, ZoomX,ZoomY,OffsetX,OffsetY : integer;
begin
  with Game.TyreList do
  begin
    Radius := 4;
    ZoomX  := 10;
    ZoomY  := 5;

    OffSetX := 80;
    OffSetY := 0;
//======= Clear Screen
    with nImage.Canvas do
    begin
      //Screen Clean
      Brush.Style := bsSolid;
      Brush.Color := clWhite;
      Pen.Style := psClear;
      Pen.Color := clBlack;
      Rectangle(0,0,nImage.Width,nImage.Height);
    end;

//======= Grid
    for T := 0 to 10 do
    begin
      with nImage.Canvas do
      begin
        Pen.Style := psDot;
        Pen.Color := clSilver;

        MoveTo((T*ZoomX*10)+OffSetX, 0);
        LineTo((T*ZoomX*10)+OffSetX, nImage.Height);
      end;
    end;

//======== Weather Line
    with nImage.Canvas do
    begin
      Pen.Style := psSolid;
      Pen.Color := clBlack;

      MoveTo((Game.Race.Weather*ZoomX)+OffSetX, 0);
      LineTo((Game.Race.Weather*ZoomX)+OffSetX, nImage.Height);
    end;

    with nImage.Canvas do
    begin
      Brush.Style := bsClear;
      Pen.Style := psSolid;
      Pen.Color := clBlack;

      nImage.Canvas.TextOut(500, 520, 'Weather');
      nImage.Canvas.TextOut(50, 250, 'Grip');
    end;

//========= Tyre Stats
    for T := 0 to High(Items) do
    begin
      for W := 0 to MAX_WEATHER do
      begin
        TyreOpZone := TyreEffect(Items[T].TOZ, W);
//        TestGrip := Round( Items[T].DryGrip * (TyreOpZone/100));
        TestGrip := Round( Items[T].DryGrip - (Items[T].DryGrip * (TyreOpZone/100)));
//        TestGrip := Round( TyreOpZone );

        case T of
          0: NewColor := clLime;
          1: NewColor := clWebGold;
          2: NewColor := clRed;
          3: NewColor := clAqua;
          4: NewColor := clOlive;
          5: NewColor := clTeal;
          6: NewColor := clFuchsia;
          else NewColor := clBlue;
        end;

        with nImage.Canvas do
        begin
          Brush.Style := bsSolid;
          Brush.Color := NewColor;
          Ellipse(((W*ZoomX)-Radius)+OffSetX,
                  ((TestGrip*ZoomY)-Radius)+OffSetY,
                  ((W*ZoomX)+Radius)+OffSetX,
                  ((TestGrip*ZoomY)+Radius)+OffSetY
                  );// := NewColor;
        end;

        nImage.Canvas.TextOut(0, 350+(T*20), Items[T].Name);

//===== Line Between Dots?
//        MoveTo(W*4, 0);
//        LineTo(Game.Race.Weather, 100);
      end;
    end;
  end;
end;

end.
